package com.zpark;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.net.URLDecoder;

/**
 * @PackageName: com.zpark
 * @ClassName: DOMUpdateXmlFileDemo
 * @Description: DOM修改xml文件
 * @author: RZS
 * @date: 2021/8/15  14:35
 */
public class DOMUpdateXmlFileDemo {
    public static void main(String[] args) throws Exception {
        // 获取xml文件对应的路径
        String path = DOMQueryXmlFileDemo.class.getClassLoader().getResource("dom-update.xml").getPath();
        // 由于此次路径中含有中文，所以需要进行解码
        path = URLDecoder.decode(path, "utf-8");
        // 获取DocumentBuilderFactory对象
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        // 根据DocumentBuilderFactory对象,构建DocumentBuilder对象
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        // 根据DocumentBuidler对象,构建Document对象
        Document document = documentBuilder.parse(path);
        // 获取文档节点的第一个子节点。如果没有这样的节点，则返回null
        Node node = document.getFirstChild();
        // 根据标签名获取标签集合，然后获取该集合中的第一个标签节点
        Node supercars = document.getElementsByTagName("supercars").item(0);
        // 获取该标签节点的所有属性
        NamedNodeMap attributes = supercars.getAttributes();
        // 获取属性为company的supercars标签节点
        Node namedItem = attributes.getNamedItem("company");
        // 设置属性值
        namedItem.setTextContent("Lamborigini");
        NodeList list = supercars.getChildNodes();
        for (int temp = 0; temp < list.getLength(); temp++) {
            Node node1 = list.item(temp);
            if (node1.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) node1;
                if ("carname".equals(eElement.getNodeName())){
                    if("Ferrari 101".equals(eElement.getTextContent())){
                        eElement.setTextContent("Lamborigini 001");
                    }
                    if("Ferrari 202".equals(eElement.getTextContent()))
                        eElement.setTextContent("Lamborigini 002");
                }
            }
        }
        NodeList childNodes = node.getChildNodes();
        for(int count = 0; count < childNodes.getLength(); count++){
            Node node2 = childNodes.item(count);
            if("luxurycars".equals(node.getNodeName()))
                node.removeChild(node2);
        }
        // write the content on console
        TransformerFactory transformerFactory =
                TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        System.out.println("-----------Modified File-----------");
        StreamResult consoleResult = new StreamResult(System.out);
        transformer.transform(source, consoleResult);


    }
}
